package com.me.mygdxgame.renderer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.me.mygdxgame.gamelogic.GameLogic;

public class RendererGL10 implements Renderer
{
	// batch to draw text
	private SpriteBatch spriteBatch;
	
	private final String fontFile = "data/default_font.fnt";
	private final String imgFile = "data/default_font.png";

	// kart
	// private StillModel kartMesh;
	// private Texture kartTexture;
	//
	// private StillModel powerUpMesh;
	// private Texture powerUpTexture;

	private String status = "";

	private final Matrix4 viewMatraix = new Matrix4();
	private final Matrix4 transformMatrix = new Matrix4();

	private Vector3 pos = new Vector3(1, 1, 1);

	private int num = 0;

	private PerspectiveCamera camera;

	public RendererGL10()
	{
		try
		{
			Gdx.app.log(RendererGL10.class.getSimpleName(), "Creating Renderer");
			spriteBatch = new SpriteBatch();

			TextureManager.getInstance().addTexture("bg","data/planet.jpg");

			FontManager.getInstance().addFont(fontFile, imgFile);

			camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(),
					Gdx.graphics.getHeight());

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void render(GameLogic logic, float dt)
	{
		GL10 gl = Gdx.graphics.getGL10();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

		renderBG();

		gl.glDisable(GL10.GL_DITHER);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glEnable(GL10.GL_CULL_FACE);

		setProjectionAndCamera(gl, pos);
		setLighting(gl);

		gl.glDisable(GL10.GL_CULL_FACE);
		gl.glDisable(GL10.GL_DEPTH_TEST);

		spriteBatch.setProjectionMatrix(viewMatraix);
		spriteBatch.setTransformMatrix(transformMatrix);

		spriteBatch.begin();

		status = "lives: " + num;
		num++;

		spriteBatch.enableBlending();
		spriteBatch.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
		AssetsManager.getInstance().get(fontFile, BitmapFont.class).draw(spriteBatch, status, 0, 320);
		spriteBatch.end();

	}

	@Override
	public void dispose()
	{
		spriteBatch.dispose();
		FontManager.getInstance().removeFont(fontFile, imgFile);

	}

	private void renderBG()
	{
		viewMatraix.setToOrtho2D(0, 0, 400, 320);
		spriteBatch.setProjectionMatrix(viewMatraix);
		spriteBatch.setTransformMatrix(transformMatrix);
		spriteBatch.begin();
		spriteBatch.disableBlending();
		spriteBatch.setColor(Color.WHITE);
		spriteBatch.draw(TextureManager.getInstance().getTexture("bg"), 0, 0, 480, 320, 0, 0, 512, 512, false,
				false);
		spriteBatch.end();
	}

	float[] lightDir = { 1, 0.5f, 0, 0 };

	private void setProjectionAndCamera(GL10 gl, Vector3 pos)
	{
		// camera.position.set(pos.x, 6, 2);
		// camera.direction.set(pos.x, 0, -4).sub(camera.position).nor();
		camera.position.set(camera.viewportWidth * .5f,
				camera.viewportHeight * .5f, 0f);
		camera.update();
		camera.apply(gl);
	}

	private void setLighting(GL10 gl)
	{
		gl.glEnable(GL10.GL_LIGHTING);
		gl.glEnable(GL10.GL_LIGHT0);
		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightDir, 0);
		gl.glEnable(GL10.GL_COLOR_MATERIAL);
	}

}
